import { createRouter, createWebHistory } from 'vue-router'
import HomeView from '../views/Home.vue'
import { ref } from 'vue'
// import Params from './params'
import nprogress from "nprogress"
import "nprogress/nprogress.css"
const dynamicroute: any = [
  {
    path: '/index',
    name: 'index',
    component: () => import('../views/index.vue'),
    meta: {
      title: '首页'
    }
  },
  {
    path: '/home/department',
    name: 'department',
    component: () => import('../views/organize.vue'),
    meta: {
      title: '组织',
    }
  },
  {
    path: '/home/role',
    name: 'role',
    component: () => import('../views/role.vue'),
    meta: {
      title: '角色'
    }
  },
  {
    path: '/home/employee',
    name: 'employee',
    component: () => import('../views/Staff.vue'),
    meta: {
      title: '员工'
    },
    children: [
      {
        path: '/home/employee/add',
        name: 'employee/add',
        component: () => import('../views/staff/staffadd.vue'),
        meta: {
          title: '添加员工',
          names: 'home'
        }
      },
      {
        path: '/home/employee/detail',
        name: 'employee/detail',
        component: () => import('../views/staff/staffdetail.vue'),
        meta: {
          title: '员工详情',
          names: 'home'
        }
      }
    ]
  },
  {
    path: '/home/permission',
    name: 'permission',
    component: () => import('../views/jurisdiction.vue'),
    meta: {
      title: '权限'
    }
  },
  {
    path: '/home/attendance',
    name: 'attendance',
    component: () => import('../views/Attendance.vue'),
    meta: {
      title: '考勤'
    }
  },
  {
    path: '/home/approval',
    name: 'approval',
    component: () => import('../views/Approval.vue'),
    meta: {
      title: '审批'
    },
    children: [
      {
        path: '/home/approval/Approvaldetail',
        name: 'home',
        component: () => import('../views/Approvaldetail.vue'),
        meta: {
          title: '审批详情'
        }
      },
    ]
  },
  {
    path: '/home/salary',
    name: 'salary',
    component: () => import('../views/Wages.vue'),
    meta: {
      title: '工资'
    },
    children: [
      {
        path: '/home/salary/detail',
        name: 'Wages/detail',
        component: () => import('../views/wages/Wagesdetails.vue'),
        meta: {
          title: '工资详情',
          names: 'home'
        }
      },
      {
        path: '/home/salary/setting',
        name: 'Wages/setting',
        component: () => import('../views/wages/setting.vue'),
        meta: {
          title: '工资设置',
          names: 'home'
        }
      }
    ]
  },
  {
    path: '/home/social',
    name: 'social',
    component: () => import('../views/Social.vue'),
    meta: {
      title: '社保'
    },
    children: [
      {
        path: '/home/social/Report',
        name: 'Report',
        component: () => import('@/views/Social/Report.vue'),
        meta: {
          title: '社保月份报表',
          names: 'home'
        }
      },
      {
        path: '/home/social/archive',
        name: 'archive',
        component: () => import('@/views/Social/archive.vue'),
        meta: {
          title: '社保历史归档',
          names: 'home'
        }
      },
      {
        path: '/home/social/detail',
        name: 'detail',
        component: () => import('@/views/Social/detailtable.vue'),
        meta: {
          title: '社保详情',
          names: 'home'
        }
      }
    ]
  }
]
const staticroute: any = [
  {
    path: '/login',
    name: 'login',
    component: () => import('../views/login.vue')
  },
  {
    path: '/',
    name: 'home',
    redirect: '/index',
    meta: {
      title: '首页',
    },
    component: HomeView,
    children: dynamicroute
  },
  {
    path: '/:pathMatch(.*)*',
    name: 'NotFound',
    component: () => import('../views/404.vue')
  }
]
import { getUserInfo, getNotice, readNotice, deleteNotice } from '@/api/home'

const router: any = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: staticroute
})
let newrouter: any = ref([])
let userData: any = ref([])
let newlist = ref(router.getRoutes().reverse().filter((item: any) => item.meta.title && item.name != 'home' && item.meta.names != 'home'))
router.beforeEach((to: any, from: any, next: any) => {
  nprogress.start();

  if (to.path == '/index') {
    nprogress.done();
    if (localStorage.getItem('token')) {
      getUserInfo().then(res => {
        res.data.data.roles.menus = [
          ...res.data.data.roles.menus,
          'index'
        ]
        router.getRoutes().forEach((item: any) => {
          if (!item.name) {
            router.removeRoute(item.name)
          }
        })
        newlist.value.forEach((item: any) => {
          if (res.data.data.roles.menus.includes(item.name)) {
            newrouter.value.push(item)
            router.addRoute({
              path: '/',
              name: 'home',
              redirect: '/index',
              meta: {
                title: '首页',
              },
              component: HomeView,
              children: newrouter.value
            })
          }
        });
      })
      next()
    } else {
      next('/login')
    }
  } else if (to.path == '/login') {

    if (localStorage.getItem('token')) {
      next('/index')
    } else {
      next()
    }
  } else {
    next()
  }

})
export default router
